package cn.tedu.utils;/**
 * @Author 金帮嘉诚 @Date 2021-07-17 星期六 14:54
 */
/*本类用于测试*/

import java.sql.*;

/**
 * 知识点
 * <p>
 * 获取链接 可以设置 要连接的 数据库名
 */
public class JdbcUtils {

    /**
     * cgb2105库 car表
     */
    public static Connection getCon() throws ClassNotFoundException, SQLException {
        String sqlPath = "com.mysql.cj.jdbc.Driver";
        Class.forName(sqlPath);
        String dataBase = "cgb2105";
        String url = "jdbc:mysql:///" + dataBase + "?characterEncoding=utf8";
        Connection connection = DriverManager.getConnection(url, "root", "root");
        return connection;

    }

    public static Connection getCon(String dataBase) throws ClassNotFoundException, SQLException {
        String sqlPath = "com.mysql.cj.jdbc.Driver";
        Class.forName(sqlPath);
//        String dataBase = "cgb2105";
        String url = "jdbc:mysql:///" + dataBase + "?characterEncoding=utf8";
        Connection connection = DriverManager.getConnection(url, "root", "root");
        return connection;
    }

    public static void closeRPC(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
